package de.tabuma.projectview.ribbon;

import java.util.*;
import java.util.logging.*;
import de.tabuma.projectview.*;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import de.tabuma.ssoclient.entity.Privileg;
import de.tabuma.pm.design.*;

/**
 *
 * @author alefen
 */
public class RibbonLinkStructurFacade {

    private static final Logger log = Logger.getLogger(RibbonLinkStructurFacade.class.getName());

    /**
     * Creates a link structur in ribbon design.<br/>
     * ____________
     * | Link 1   |  Link2   Link3   Link4
     * |          |______________________________________
     * |                                                |
     * | ----------  ---------   ----------  ---------- |
     * | | Link11 |  | Link12 |  | Link13 |  | Link14 | |
     * | ----------  ----------  ----------  ---------- |
     * --------------------------------------------------
     * @param projectId
     * @return
     */

    public static String getLinkStructur(int projectId, String ssoId, String setting, String design, String current, int designId) {

        List<ProjectNavigation> list = ProjectNavigationFacade.getList(projectId);

        List<Design> dList = DesignFacade.getList(projectId);

        ProjectNavigation pn = new ProjectNavigation();
        ProjectNavigationWrapper pnw = new ProjectNavigationWrapper(pn, projectId);
        pnw.setNavigationId(0);
        pnw.setId(new Long(0));
        pnw.loadSubElement(list);
        
        List<de.tabuma.ssoclient.entity.Privileg> pList = null;
        if(ssoId!=null) {
            try {
                pList = de.tabuma.ssoclient.logik.SSOFacade.getUserPrivileges(ssoId);
            }
            catch(Exception e) {

            }
        }


        String str = "";
        str += "\t<div class=\"mainContainer\">\n";
        str += "\t\t<ul class=\"ribbon\">\n";
        str += "\t\t\t<li>\n";
        str += "\t\t\t\t<ul class=\"orb\">\n";
        str += "\t\t\t\t\t<li><a href=\"javascript:void(0);\" accesskey=\"1\" class=\"orbButton\">&nbsp;</a><span>Menu</span>\n";
        str += "\t\t\t\t\t\t<ul>\n";
        if(ssoId!=null) {
            str += "\t\t\t\t\t\t<li><a href=\""+constants.projectview+"?"+constants.ssoId+"="+ssoId+"&"+constants.logout+"=true&"+constants.projectId+"="+projectId+"\">Logout</a></li>\n";
        }
        for(int i=0;i<dList.size();i++) {
            Design d = dList.get(i);
            if(d.getId().intValue()==designId) {
                str += "\t\t\t\t\t\t<li><a href=\""+constants.projectview+"?"+constants.ssoId+"="+ssoId+"&"+constants.changeDesign+"="+d.getId().intValue()+"&"+constants.projectId+"="+projectId+"\">"+design+" ("+current+") "+d.getName()+"</a></li>\n";
            }
            else {
                str += "\t\t\t\t\t\t<li><a href=\""+constants.projectview+"?"+constants.ssoId+"="+ssoId+"&"+constants.changeDesign+"="+d.getId().intValue()+"&"+constants.projectId+"="+projectId+"\">"+design+" "+d.getName()+"</a></li>\n";
            }
        }
        str += "\t\t\t\t\t\t</ul>\n";
        str += "\t\t\t\t\t</li>\n";
        str += "\t\t\t\t</ul>\n";
        str += "\t\t\t</li>\n";
        str += "\t\t\t<li>\n";
        str += "\t\t\t\t<ul class=\"menu\">\n";
        for(int i=0;i<pnw.getSubElements().size();i++) {
            ProjectNavigationWrapper pnw1 = pnw.getSubElements().get(i);

            String name = getRibbonName(pn.getName());

            if(userHasPrivileg(pnw1, pList) || ssoId==null) {
                str += "\t\t\t\t\t<li><a href=\"#"+name+"\">"+pnw1.getName()+"</a>\n";
                str += "\t\t\t\t\t\t<ul>\n";

                if(pnw1.getNavMode()!=0) {
                    str += "\t\t\t\t\t\t\t<li>\n";
                    str += "\t\t\t\t\t\t\t\t<h2>\n";
                    str += "\t\t\t\t\t\t\t\t\t<span>"+pnw1.getName()+"</span>\n";
                    str += "\t\t\t\t\t\t\t\t</h2>\n";
                    str += "\t\t\t\t\t\t\t\t\t<div class=\"navigationelement"+pnw1.getId()+"\" onclick=\"javascript:parent."+constants.functionName+pnw1.getId()+"()\">"+pnw1.getName()+"</div>\n";
                    str += "\t\t\t\t\t\t\t</li>\n";
                }


                //Step 2
                if(pnw1.getSubElements().size()>0) {
                    for(int j=0;j<pnw1.getSubElements().size();j++) {
                        ProjectNavigationWrapper pnw2 = pnw1.getSubElements().get(j);

                        if(userHasPrivileg(pnw2, pList) || ssoId==null) {

                            if(pnw2.getNavMode()!=0 || pnw2.getSubElements().size()>0) {
                                str += "\t\t\t\t\t\t<li>\n";
                                str += "\t\t\t\t\t\t\t<h2>\n";
                                str += "\t\t\t\t\t\t\t\t<span>"+pnw2.getName()+"</span>\n";
                                str += "\t\t\t\t\t\t\t\t</h2>\n";
                                if(pnw2.getNavMode()!=0) {
                                    str += "\t\t\t\t\t\t\t\t\t<div class=\"navigationelement"+pnw2.getId()+"\" onclick=\"javascript:parent."+constants.functionName+pnw2.getId()+"()\">"+pnw2.getName()+"</div>\n";
                                }

                                for(int k=0;k<pnw2.getSubElements().size();k++) {
                                    ProjectNavigationWrapper pnw3 = pnw2.getSubElements().get(k);

                                    if(userHasPrivileg(pnw3, pList) || ssoId==null) {
                                        if(pnw3.getNavMode()!=0) {
                                            str += "\t\t\t\t\t\t\t\t\t<div class=\"navigationelement"+pnw3.getId()+"\" onclick=\"javascript:parent."+constants.functionName+pnw3.getId()+"()\">"+pnw3.getName()+"</div>\n";
                                        }
                                        List<ProjectNavigationWrapper> myList = getLinkStructur(pnw3);
                                        if(myList.size()>0) {
                                            str += "\t\t\t\t\t\t\t\t\t<div class=\"ribbon-list\"><div>"+pnw3.getName()+ " subelements\n";
                                            str += "\t\t\t\t\t\t\t\t\t<ul>\n";
                                            for(int l=0;l<myList.size();l++) {
                                                ProjectNavigationWrapper pnw4 = myList.get(l);
                                                if(userHasPrivileg(pnw4,pList) || ssoId==null) {
                                                    if(pnw4.getNavMode()!=0) {
                                                        str += "\t\t\t\t\t\t\t\t\t\t<li class=\"navigationelement"+pnw4.getId()+"\" onclick=\"javascript:parent."+constants.functionName+pnw4.getId()+"()\">"+pnw4.getName()+"</li>\n";
                                                    }
                                                }
                                            }
                                            str += "\t\t\t\t\t\t\t\t\t</ul>\n";
                                            str += "\t\t\t\t\t\t\t\t</div>\n";
                                            str += "\t\t\t\t\t\t\t</div>\n";
                                        }
                                    }
                                }

                                str += "\t\t\t\t\t\t\t</li>\n";
                            }
                        }
                    }
                }

                str += "\t\t\t\t\t\t</ul>\n";
                str += "\t\t\t\t\t</li>\n";
            }
        }
        str += "\t\t\t\t</ul>\n";
        str += "\t\t\t</li>\n";
        str += "\t\t</ul>\n";
        str += "\t</div>\n";
        return str;
    }

    public static String getRibbonName(String name) {
        if(name==null) {
            return "";
        }
        name = name.replaceAll(" ","_");
        return name;
    }

    public static List<ProjectNavigationWrapper> getLinkStructur(ProjectNavigationWrapper pnw) {
        List<ProjectNavigationWrapper> list = new ArrayList<ProjectNavigationWrapper>();
        for(int i=0;i<pnw.getSubElements().size();i++) {
            list.add(pnw.getSubElements().get(i));
            List<ProjectNavigationWrapper> myList = getLinkStructur(pnw.getSubElements().get(i));
            list.addAll(myList);
        }
        return list;
    }

    public static boolean userHasPrivileg(ProjectNavigation pn, List<Privileg> pList) {
        String privileg = de.tabuma.pm.constants.PROJECTNAVIGATION+" "+pn.getName()+" ("+pn.getId()+")";
        if(pList==null) {
            return false;
        }
        for(int i=0;i<pList.size();i++) {
            String privilegName = pList.get(i).getName();
            log.warning(privileg+"/"+privilegName);
            if(privilegName.equals(privileg)) {
                return true;
            }
        }
        return false;
    }
}